from datetime import datetime
import os,sys

import execelUtil.excute_abstract as excuteabc
import execelUtil.common.commonUtil as comutils

# 目标URL


root_path = ""
if os.name == 'nt':
    root_path = os.path.dirname(os.path.realpath(sys.argv[0]))+'\\template\\'
else:
    root_path = '/home/root_hm/project/project_python/inventory-small-tool-backend/extFile/'
print(root_path)

filename = root_path+"tmp-occoupy.ods"



class OccpySalesClass:


    req_exam = comutils.SALES_ORDER_JIE_DAN

    def __init__(self):
        super().__init__()


    def execute_sku(self,skulist):
        for sku in skulist:
            qty = int(sku["holdQty"])
            sku["useQty"] = qty*-1
        return skulist

    def execute_release_hold_sku(self,skulist):
        for sku in skulist:
            qty = int(sku["holdQty"])
            sku["holdQty"] = qty*-1
            sku["totalQty"] = qty*-1
            sku["useQty"] = 0
        return skulist

    def excute_req(self, req_exam,skucode=None, bizType=None):
        # BillList = comutils.getBillList()
        # if req_exam["originBillNo"] not in BillList:
        #     req_exam=None
        #     return
        result = comutils.getSalesOrders(req_exam["originBillNo"])
        skuList = req_exam["skuList"]
        # 释放预占库存
        # if 'orderStatus' in result and result["orderStatus"] == 99:
        req_exam["skuList"] = self.execute_release_hold_sku(skuList)
        # else:
            # print(req_exam)
            # req_exam = None
        return req_exam



class DBTransitClass:
    req_exam = comutils.TRANSFER_OUT_TRANSIT

    def excute_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty * -1
            sku["holdQty"] = 0
            sku["totalQty"] = 0
        return skuList

    def excute_rever_hold_sku(self, skuList):
        for sku in skuList:
            sku["holdQty"] = sku["holdQty"] * -1
            sku["totalQty"] = sku["holdQty"]
        return skuList


    def excute_release_hold_sku(self, skuList):
        for sku in skuList:
            sku["useQty"] = sku["holdQty"]
            sku["holdQty"] = sku["holdQty"] * -1
            sku["totalQty"] =0
        return skuList



    def excute_bad_hold_sku(self, skuList,whCode):
        for sku in skuList:
            qty = int(sku["holdQty"])
            res= comutils.getStockByskuCodeAndWhcode(sku["skuCode"],sku['oldSkuCode'], whCode, 1, 10)
            if(res['badHoldQty']+res['badQty']<qty):
                print(sku["skuCode"]+" 库存总表的不良品占用小于当前占用数量")
                continue
            sku["badHoldQty"] = qty * -1
            sku["totalQty"] = qty * -1
            sku["inTransitQty"] = 0
            sku["holdQty"] = 0
        return skuList

    def excute_bad_putaway_hold_sku(self, skuList, whCode):
        for sku in skuList:
            qty = int(sku["holdQty"])
            res = comutils.getStockByskuCodeAndWhcode(sku["skuCode"], sku['oldSkuCode'], whCode, 1, 10)
            if (res['badHoldQty'] + res['badQty'] < qty):
                print(sku["skuCode"] + " 库存总表的不良品占用小于当前占用数量")
                continue
            sku["badHoldQty"] = qty * -1
            sku["totalQty"] = 0
            sku["inTransitQty"] = 0
            sku["holdQty"] = 0
            sku["badQty"] = qty
        return skuList

    def excute_req(self, req_exam, errorSku=None, bizType=None):
        now_date = datetime.now()
        skuList = req_exam["skuList"]
        whCode = req_exam["warehouseCode"]

        opertypes = comutils.getOperType()
        print(req_exam["operationTypeEnum"])
        req_exam["billTypeEnum"] = "OTHER_OUTBOUND"
        req_exam["skuList"] = self.excute_release_hold_sku(skuList)
        # if bizType!=None and  bizType.startswith("BAD"):
        #     self.excute_bad_hold_sku(skuList)
        #     return req_exam
        # elif bizType!=None and  bizType.startswith("GOOD"):
        #     self.excute_rever_hold_sku(skuList)
        #     return req_exam
        # self.excute_rever_hold_sku(skuList)
        # if opertype in opertypes:
        #     req_exam["operationTypeEnum"] = opertypes[req_exam["operationTypeEnum"]]
        #     if "OUT_HOLD" in opertype:
        #         if opertype.startswith("BAD"):
        #             req_exam["skuList"] = self.excute_bad_hold_sku(skuList)
        #         else:
        #             req_exam["skuList"] = self.excute_rever_hold_sku(skuList)
        #     elif "PUWAWAY_HOLD" in opertype :
        #         if opertype.startswith("BAD"):
        #             req_exam["skuList"] = self.excute_bad_hold_sku(skuList)
        #         else:
        #             req_exam["skuList"] = self.excute_rever_hold_sku(skuList)
        #         print(opertype)
        return req_exam




class OccpyWmsClass:

    #销售订单
    # req_exam = comutils.SALES_ORDER_OUT
    #其他出
    req_exam = comutils.OTHER_INBOUND

    def __init__(self):
        super().__init__()

    def excute_2_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            sku["holdQty"] = qty * -1
            sku["totalQty"] = qty * -1
        return skuList,"HOLD"

    def excute__1_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            sku["holdQty"] = qty * -1
            sku["useQty"] = qty * 1
            sku["totalQty"] = 0
        return skuList,"HOLD"

    def execute_release_bad_putaway_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,badHoldQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "PUTAWAY"

    def execute_release_bad_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,badHoldQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "HOLD"


    def execute_release_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,holdQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "HOLD"

    def execute_conver_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "useQty,holdQty":
                        if prop == 'useQty':
                            sku[prop] = sku[prop]
                        else:
                            sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "HOLD"

    def execute_conver_putaway_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "useQty,holdQty":
                        if prop == 'useQty':
                            sku[prop] = sku[prop]
                        else:
                            sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "PUTAWAY"

    def excute_req(self, req_exam,skucode=None, bizType=None):
        skuList = req_exam["skuList"]
        # BillList = comutils.getBillList()
        # if req_exam["originBillNo"] not in BillList:
        #     req_exam=None
        #     return
        # result = comutils.getSalesOrders(req_exam["originBillNo"])
        # print(result)
        req_exam["skuList"], optype = self.execute_conver_putaway_sku(skuList)
        req_exam["operationTypeEnum"] = optype
        # result = dict()
        # skuList = req_exam["skuList"]
        # if 'orderStatus' in result and result["orderStatus"] == 2:
        #     billNo = result["warehouseOrderId"]
        #     req_exam["billNo"] = billNo
        #     req_exam["remark"] = billNo
        #     req_exam["skuList"] = self.excute_2_sku(skuList)
        # elif 'orderStatus' in result and result["orderStatus"] == -1:
        #     req_exam["skuList"] = self.excute__1_sku(skuList)
        #     req_exam["billTypeEnum"] = "SALE_OUTBOUND_ORDERS"
        # elif 'orderStatus' in result and result["orderStatus"] == 99:
        #     req_exam["skuList"] = self.excute__1_sku(skuList)
        #     req_exam["billTypeEnum"] = "SALE_OUTBOUND_ORDERS"
        # else:
        #     print(req_exam)
        #     req_exam = None
        return req_exam


class nullBillClass:
    req_exam=None
    def __init__(self):
        super().__init__()

    def excute_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            for pp in sku :
                if pp == "inTransitQty":
                     sku[pp] = qty * -1
                elif pp.endswith("Qty"):
                    sku[pp] = 0

        return "HEAD_IN_TRANSIT"

    def excute_release_qco_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,holdQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "QCO"

    def excute_release_out_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,holdQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "HOLD"

    def excute_release_putaway_hold_sku(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,holdQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "PUTAWAY"

    def excute_rever_hold_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            sku["holdQty"] = qty * -1
            sku["useQty"] = qty
            sku["totalQty"] = 0
        return skuList

    def excute_release_bad_hold_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            sku["badHoldQty"] = qty * -1
            sku["totalQty"] = qty * -1
            sku["holdQty"] = 0
        return skuList

    # 回退不良品占用
    def excute_reverse_bad_hold_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            sku["badHoldQty"] = qty * -1
            sku["badQty"] = qty
        return skuList

    def excute_req(self, req_exam, skuCode=None, bizType=None):
        now_date = datetime.now()
        billNo = now_date.strftime("%y%m%d") + "_inv_fix_occpy"
        req_exam["originBillNo"] = billNo
        req_exam["remark"] = billNo
        req_exam["billNo"] = billNo
        skuList = req_exam["skuList"]
        whcode = req_exam["warehouseCode"]
        opertypes = comutils.getOperType()
        print(req_exam["operationTypeEnum"])
        # req_exam["billTypeEnum"] = "HEAD_IN_TRANSIT"
        t_skuList,opertypes= self.excute_release_putaway_hold_sku(skuList)

        req_exam["skuList"]=t_skuList
        req_exam["operationTypeEnum"]=opertypes
        # opertype = str(req_exam["operationTypeEnum"])
        # if opertype in opertypes:
        #     req_exam["operationTypeEnum"] = opertypes[req_exam["operationTypeEnum"]]
        #     if "OUT_HOLD" in opertype:
        #         if opertype.startswith("BAD"):
        #             req_exam["skuList"] = self.excute_bad_hold_sku(skuList)
        #         else:
        #             req_exam["skuList"] = self.excute_rever_hold_sku(skuList)
        #     elif "PUWAWAY_HOLD" in opertype :
        #         print(opertype)
        return req_exam

#头程出运
class HeadTransShipClass:
    req_exam = comutils.HEAD_TRANS_SHIPOUT


    def __init__(self):
        super().__init__()


    def excute_ship_hold_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["holdQty"])
            sku["holdQty"] = qty * -1
            sku["totalQty"] = qty * -1
        return skuList



    def excute_req(self, req_exam,skuCode=None,biztype=None):
        billNo = req_exam["billNo"]
        req_exam["originBillNo"] = billNo
        skuList = req_exam["skuList"]
        req_exam["skuList"] = self.excute_ship_hold_sku(skuList)
        return req_exam


#采购生产在途
class PurchaseTransitClass:
    req_exam = comutils.PURCHASE_PRODUCE_TRANSIT

    def __init__(self):
        super().__init__()

    def excute_pur_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty
        return skuList

    def excute_release_pur_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty*-1
        return skuList

    def excute_req(self, req_exam,skuCode=None,biztype= None):
        billNo = req_exam["billNo"]
        req_exam["originBillNo"] = billNo
        skuList = req_exam["skuList"]
        req_exam["skuList"] = self.excute_release_pur_transit_sku(skuList)
        return req_exam



#采购收货在途
class PuchaseReceiveTransitClass:

    req_exam = comutils.PURCHASE_RECEIVE_TRANSIT

    def __init__(self):
        super().__init__()

    def excute_pur_receivce_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty
        return skuList

    def excute_release_pur_receivce_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty*-1
        return skuList

    def excute_req(self, req_exam,skuCode=None,bizType=None):
        billNo = req_exam["billNo"]
        req_exam["originBillNo"] = billNo
        req_exam["operationTypeEnum"] = "PUR_RECEIVE_IN_TRANSIT"
        skuList = req_exam["skuList"]
        req_exam["skuList"] = self.excute_release_pur_receivce_transit_sku(skuList)
        return req_exam



#头程入库
class HeadTransInbound:

    req_exam = comutils.HEAD_SHIPMENT

    def __init__(self):
        super().__init__()

    def excute_pur_receivce_transit_sku_from_transit(self, skuList):
        for sku in skuList:
            for prop in sku:
                if prop.endswith("Qty") and sku[prop] != 0:
                    if prop in "totalQty,useQty":
                        sku[prop] = sku[prop]
                    elif prop in "inTransitQty":
                        sku[prop] = sku[prop] * -1
                    else:
                        sku[prop] = 0
        return skuList, "HEAD_IN_TRANSIT"


    def excute_pur_receivce_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty * -1
        return skuList

    def excute_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty * -1
            sku["holdQty"] = 0
            sku["totalQty"] = 0
        return skuList

    def excute_req(self, req_exam,skuCode=None,bizType=None):

        skuList = req_exam["skuList"]
        req_exam["skuList"],optertype = self.excute_pur_receivce_transit_sku_from_transit(skuList)
        req_exam["operationTypeEnum"] = optertype
        return req_exam



class wmsHeadTransClass:

    req_exam = comutils.HEAD_TRANS_OUT

    def __init__(self):
        super().__init__()


    def execute_bad2good_sku(self, skuList):
        for sku in skuList:
            qty = sku["holdQty"]
            sku["holdQty"] = qty*-1
            sku["useQty"] = qty
            sku["totalQty"] = qty
        return skuList

    def execute_releaseHold_sku(self, skuList):
        for sku in skuList:
            qty = sku["holdQty"]
            if qty > 0:
                qty=qty*-1
            sku["holdQty"] = qty
            sku["useQty"] = qty*-1
            sku["totalQty"] = 0
        return skuList

    def execute_outHold_sku(self, skuList):
        for sku in skuList:
            qty = sku["holdQty"]
            if qty > 0:
                qty = qty * -1
            sku["holdQty"] = qty
            sku["totalQty"] = qty
        return skuList

    def execute_only_outHold_sku(self, skuList):
        for sku in skuList:
            qty = sku["holdQty"]
            if qty > 0:
                qty = qty * -1
            sku["holdQty"] = qty
            sku["totalQty"] = 0
        return skuList

    def excute_req(self, req_exam,skuCode=None, biztype=None):
        # print(req_exam["billNo"])
        # org_bill = req_exam["billNo"]
        # bills = comutils.getBillList()
        # if org_bill not in bills:
        #     return None
        skulist = req_exam["skuList"]
        req_exam["skuList"],opertype = self.excute_pur_receivce_transit_sku_from_transit(skulist)
        req_exam["operationTypeEnum"] = opertype
        return req_exam



#头程入库
class HeadTransit:

    req_exam = comutils.HEAD_SHIPMENT

    def __init__(self):
        super().__init__()

    def excute_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty
        return skuList

    def excute_release_transit_sku(self, skuList):
        for sku in skuList:
            qty = int(sku["inTransitQty"])
            sku["inTransitQty"] = qty * 1
            sku["holdQty"] = 0
            sku["totalQty"] = 0
        return skuList

    def excute_req(self, req_exam, skuCode=None, biztype=None):
        skuList = req_exam["skuList"]
        req_exam["skuList"] = self.excute_release_transit_sku(skuList)
        req_exam["operationTypeEnum"] = "HEAD_IN_TRANSIT"
        return req_exam



# result = comutils.getSalesOrders('GOC81143441')


wmsOccpy = HeadTransInbound()
result = excuteabc.excute_occpy(filename, wmsOccpy.req_exam, wmsOccpy, 0, True)
print(result)

